#include <iostream>
#include <fstream>
#include <string>
#include "contacts.pb.h"
using namespace std;
void AddPeopleInfo(contacts::PeoleInfo *contact)
{
    std::string name;
    std::cout << "-------------新增联系⼈-------------" << std::endl;
    std::cout << "请输⼊联系⼈姓名: ";
    getline(std::cin, name);
    contact->set_name(name);
    std::cout << "请输⼊联系⼈年龄: ";
    int age;
    std::cin >> age;
    contact->set_age(age);
    // 清理输入缓冲区。
    std::cin.ignore(256, '\n');
    for (int i = 1;; i++)
    {
        std::cout << "请输⼊联系⼈电话" << i << "(只输⼊回⻋完成电话新增): ";
        std::string number;
        getline(std::cin, number);
        if (number.empty())
        {
            break;
        }
        contacts::PeoleInfo_Phone *phone = contact->add_phone();
        phone->set_number(number);
        std::cout << " 请输入该电话类型(1、移动电话   2、固定电话): ";
        int type;
        std::cin >> type;
        std::cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            phone->set_type(contacts::PeoleInfo::Phone::PhoneType::PeoleInfo_Phone_PhoneType_MP);
            break;
        case 2:
            phone->set_type(contacts::PeoleInfo::Phone::PhoneType::PeoleInfo_Phone_PhoneType_TEL);
            break;
        default:
            std::cout << "类型错误" << std::endl;
            break;
        }
    }
    contacts::Address address;
    cout << "请输入联系人家庭地址：";
    string home_address;
    getline(cin, home_address);
    address.set_home_address(home_address);
    cout << "请输入联系人单位地址：";
    string unit_address;
    getline(cin, unit_address);
    address.set_unit_address(unit_address);

    cout << "请选择要添加的其他联系方式：1.qq，2.微信" << endl;
    int other_contacts = 0;
    cin >> other_contacts;
    cin.ignore(256, '\n');
    if (other_contacts == 1)
    {
        string qq;
        cout << "请输入qq号：";
        getline(cin,qq);
        contact->set_qq(qq);
    }else if(2==other_contacts)
    {
        string wechat;
        cout<<"请输入微信号：";
        getline(cin,wechat);
        contact->set_wechat(wechat);
    }else{
        cout<<"选择有误，并未设置成功其他类型的联系方式"<<endl;
    }

     for(int i=0;;i++)
     {
        cout<<"输入备注的"<<i+1<<"的主题(直接输入回车表示完成输入)";
        string key;
        getline(cin,key);
        if(key.empty())
        {
            break;
        }
        string value;
        cout<<"输入"<<i+1<<"主题的备注信息";
        getline(cin,value);
        contact->mutable_remark()->insert({key,value});
     }
    std::cout << "-----------添加联系⼈成功-----------" << std::endl;
}
int main()
{
    contacts::Contacts contacts;
    std::fstream input("contacts.bin", std::ios::in | std::ios::binary);
    if (!input)
    {
        std::cout << "文件不存在，创建了该文件" << std::endl;
    }
    else if (!contacts.ParseFromIstream(&input))
    {
        std::cout << "parse error!" << std::endl;
        input.close();
        return -1;
    }
    // 想通讯录里卖添加一个联系人
    AddPeopleInfo(contacts.add_contacts());
    std::fstream output("contacts.bin", std::ios::out | std::ios::binary);
    if (!contacts.SerializePartialToOstream(&output))
    {
        std::cerr << "write error" << std::endl;
        input.close();
        output.close();
        return -1;
    }
    std::cout << "write success" << std::endl;
    input.close();
    output.close();
    return 0;
}